<!--
 * @Author: Freude Xu
 * @Date: 2021-10-06 15:46:46
 * @LastEditors: Freude Xu
 * @LastEditTime: 2021-10-06 16:10:05
 * @Description: 京东快递单号查询案例
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>京东快递单号查询</title>
    <style>
        body {
            margin: 0;
            padding: 0;
        }
        
        .search {
            position: relative;
            width: 178px;
            margin: 100px;
        }
        
        .con {
            display: none;
            position: absolute;
            top: -40px;
            width: 171px;
            border: 1px solid rgba(0, 0, 0, .2);
            box-shadow: 0.2px 4px rgba(0, 0, 0, .2);
            padding: 5px 0;
            font-size: 18px;
            line-height: 20px;
            color: #333;
        }
        /* 三角形 */
        
        .con::before {
            content: '';
            width: 0;
            height: 0;
            position: absolute;
            top: 28px;
            left: 18px;
            border: 8px solid #000;
            border-style: solid dashed dashed;
            border-color: #fff transparent transparent;
        }
    </style>
</head>

<body>
    <div class="search">
        <div class="con">123</div>
        <input type="text" class="jd" placeholder="请输入您的快递单号">
    </div>
</body>
<script>
    var con = document.querySelector('.con');
    var jd_input = document.querySelector('.jd');
    //不使用keydown和keypress的原因：他们在文本框中的特点，当两个事件触发时，文字还没有落入文本框中
    //keyup事件触发时，文字已经落入文本框里面了
    jd_input.addEventListener('keyup', function(e) {
            //当文本框中没有东西时不展示大号盒子
            if (this.value == '') {
                con.style.display = 'none';
            } else {
                //展示大号盒子并取得文本框中的内容
                con.style.display = 'block';
                con.innerText = this.value;
            }
        })
        //当失去焦点时大号盒子隐藏
    jd_input.addEventListener('blur', function(e) {
            con.style.display = 'none';
        })
        //当获得焦点时并且内容不为空时将大号盒子展示
    jd_input.addEventListener('focus', function(e) {
        if (this.value !== '') {
            con.style.display = 'block';
        }
    })
</script>

</html>